{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('12.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "<meta charset=\"utf-8\"/>\n",
       "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       "<title>标签节点</title>\n",
       "</head>\n",
       "<body>\n",
       "<div id=\"div1\">\n",
       "<p id=\"p\">p1</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "</div>\n",
       "<div id=\"div2\">\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a2\">a2</a>\n",
       "</div>\n",
       "</body>\n",
       "</html>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"ac\" href=\"a1\">a1</a>, <a class=\"ac\" href=\"a2\">a2</a>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.find_all('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "atags = sp.find_all('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a class=\"ac\" href=\"a1\">a1</a>\n",
      "<a class=\"ac\" href=\"a2\">a2</a>\n"
     ]
    }
   ],
   "source": [
    "## 所有a标签被移除\n",
    "for a in atags:\n",
    "    print(a.extract())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "<meta charset=\"utf-8\"/>\n",
       "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       "<title>标签节点</title>\n",
       "</head>\n",
       "<body>\n",
       "<div id=\"div1\">\n",
       "<p id=\"p\">p1</p>\n",
       "\n",
       "</div>\n",
       "<div id=\"div2\">\n",
       "<p id=\"p\">p2</p>\n",
       "\n",
       "</div>\n",
       "</body>\n",
       "</html>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<p id=\"p\">p1</p>, <p id=\"p\">p2</p>]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 剔除所有的p标签\n",
    "[p.extract() for p in sp.find_all('p')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "<meta charset=\"utf-8\"/>\n",
       "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       "<title>标签节点</title>\n",
       "</head>\n",
       "<body>\n",
       "<div id=\"div1\">\n",
       "\n",
       "\n",
       "</div>\n",
       "<div id=\"div2\">\n",
       "\n",
       "\n",
       "</div>\n",
       "</body>\n",
       "</html>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<html lang=\"en\">\n",
       " \n",
       " \n",
       " </html>, <head>\n",
       " \n",
       " \n",
       " \n",
       " \n",
       " </head>, <meta charset=\"utf-8\"/>, <meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>, <meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>, <title>标签节点</title>, <body>\n",
       " \n",
       " \n",
       " </body>, <div id=\"div1\">\n",
       " \n",
       " \n",
       " </div>, <div id=\"div2\">\n",
       " \n",
       " \n",
       " </div>]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 剔除所有的标签\n",
    "[i.extract() for i in sp.find_all()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<!DOCTYPE html>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 剔除父节点后，子节点也会被剔除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
